import 'package:business_user/logic/user_logic.dart';
import 'package:flutter/material.dart';
import 'package:lib_core/lib_core.dart';
import 'package:lib_ui/lib_ui.dart';
import 'package:lib_ui/widget/main_button.dart';

class LoginPage extends StatelessWidget {
  LoginPage({super.key});

  String username = 'sleepingcat';
  String password = '11111aaaaa';
  late final logic = Get.put(UserLogic());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TransparentAppBar(title: '登录'),
      body: Container(
        alignment: Alignment.topCenter,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            _buildInputAccount(),
            const SizedBox(height: 10),
            _buildInputPassword(),
            const SizedBox(height: 10),
            SizedBox(
                width: 100,
                child: MainButton(
                  text: '登录',
                  onClick: () async {
                    if (username.isEmpty) {
                      '请输入账号'.showError();
                      return;
                    }
                    if (password.isEmpty) {
                      '请输入密码'.showError();
                      return;
                    }
                    final userModel = await logic.login(username, password);
                    if (userModel != null) {
                      await '登录成功'.showSuccess();
                      BoostNavigator.instance.pop();
                    }
                  },
                )),
          ],
        ),
      ),
    );
  }

  _buildInputAccount() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(
          '输入账号：',
          style: LDFont.regular(),
        ),
        SizedBox(
          width: 200,
          height: 40,
          child: CommonTextField(
            text: username,
            type: CommonTextFieldType.clear,
            onSubmitted: (str) {
              username = str;
            },
          ),
        ),
      ],
    );
  }

  _buildInputPassword() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(
          '输入密码：',
          style: LDFont.regular(),
        ),
        SizedBox(
          width: 200,
          height: 40,
          child: CommonTextField(
            text: password,
            type: CommonTextFieldType.password,
            onSubmitted: (str) {
              password = str;
            },
          ),
        ),
      ],
    );
  }
}
